﻿using System.Linq;
using System.Threading.Tasks;
using OneoneNine.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
using OneoneNine.ViewModels;

namespace Heavy.Web.Controllers
{
    public class UserController : Controller
    {
        private readonly UserManager<IdentityUser> _userManager;

        public UserController(UserManager<IdentityUser> userManager)
        {
            _userManager = userManager;
        }

        // [RequireHttps]
        public async Task<IActionResult> Index(string name)
        {
            var examineList = await _userManager.Users
                   .Where(e => e.UserName == name) // 使用 name 进行筛选
                   .ToListAsync();

            return View(examineList);
        }

        public IActionResult AddUser()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> AddUser(UserAddViewModel userAddViewModel)
        {
            if (!ModelState.IsValid)
            {
                return View(userAddViewModel);
            }

            var user = new IdentityUser
            {
                UserName = userAddViewModel.UserName,
                Email = userAddViewModel.Email,
            };

            var result = await _userManager.CreateAsync(user, userAddViewModel.Password);

            if (result.Succeeded)
            {
                return RedirectToAction("Index", await _userManager.Users.ToListAsync());
            }

            foreach (IdentityError error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            return View(userAddViewModel);

        }

        [HttpPost]
        public async Task<IActionResult> DeleteUser(string id)
        {
            var user = await _userManager.FindByIdAsync(id);
            if (user != null)
            {
                var result = await _userManager.DeleteAsync(user);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }

                ModelState.AddModelError(string.Empty, "删除用户时发生错误");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "用户找不到");
            }

            return View("Index", await _userManager.Users.ToListAsync());
        }

        public async Task<IActionResult> EditUser(string id)
        {
            var user = await _userManager.FindByIdAsync(id);
            if (user == null)
            {
                return RedirectToAction("Index");
            }

            var claims = await _userManager.GetClaimsAsync(user);

            var vm = new UserEditViewModel
            {
                Id = user.Id,
                Email = user.Email,
                UserName = user.UserName,
                Claims = claims.Select(x => x.Value).ToList()
            };

            return View(vm);
        }

        //[HttpPost]
        //public async Task<IActionResult> EditUser(string id, UserEditViewModel userEditViewModel)
        //{
        //    var user = await _userManager.FindByIdAsync(id);
        //    if (user == null)
        //    {
        //        return RedirectToAction("Index");
        //    }

        //    user.UserName = userEditViewModel.UserName;
        //    user.Email = userEditViewModel.Email;

        //    var result = await _userManager.UpdateAsync(user);

        //    if (result.Succeeded)
        //    {
        //        return RedirectToAction("Index");
        //    }
        //    ModelState.AddModelError(string.Empty, "更新用户信息时发生错误");
        //    return View(userEditViewModel);
        //}

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditUser(int id, [Bind("UserName,Email")] IdentityUser users)
        {
            if (ModelState.IsValid)
            {
                await _userManager.UpdateAsync(users);

                return RedirectToAction(nameof(Index));
            }
            return View();
        }
    }
}
